package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.IBorrowInBackService;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Service
public class BorrowInBackServiceImpl extends ServiceImpl<BorrowInBackMapper, BorrowInBack> implements IBorrowInBackService {
    @Autowired
    BorrowInBackMapper borrowInBackMapper;
    @Autowired
    BorrowInBackGoodsMapper borrowInBackGoodsMapper;
    @Autowired
    GoodsSnMapper goodsSnMapper;
    @Autowired
    GoodsSnLogMapper goodsSnLogMapper;
    @Autowired
    GoodsStockMapper goodsStockMapper;
    @Autowired
    HouseWaitOutMapper houseWaitOutMapper;
    @Autowired
    HouseWaitOutGoodsMapper houseWaitOutGoodsMapper;
    @Autowired
    BorrowInMapper borrowInMapper;
    @Autowired
    BorrowInGoodsMapper borrowInGoodsMapper;


    @Override
    public boolean checkNo(String no, String companyId) {
        return borrowInBackMapper.selectCount(new QueryWrapper<BorrowInBack>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public void insert(BusinessSet businessSet, BorrowInBack borrowInBack, List<BorrowInBackGoods> borrowInBackGoodsList, List<GoodsSn> goodsSnList, List<GoodsSnLog> goodsSnLogList) {
        borrowInBackMapper.insert(borrowInBack);
        borrowInBackGoodsMapper.batchInsert(borrowInBackGoodsList);

        BorrowInBackVo borrowInBackVo = borrowInBackMapper.getVoById(borrowInBack.getId());

        processBorrowIn(borrowInBackVo);

        if (!PdosUtils.houseInOut(businessSet)) {
            //未启用出入库管理，直接添加库存
            processGoodsStock(borrowInBackVo);
            if (!Utils.isEmpty(goodsSnList)) {
                goodsSnMapper.outByList(borrowInBack.getHouseId(), goodsSnList);
                goodsSnLogMapper.insertBatch(goodsSnLogList);
            }
        } else {
            //启用库存管理，则添加待出库单数据
            processHouseWaitOut(borrowInBackVo);
        }
    }

    @Override
    public BorrowInBackVo getVoById(String id) {
        return borrowInBackMapper.getVoById(id);
    }

    @Override
    public List<BorrowInBackVo> listVoByBorrowInId(String borrowInId) {
        return borrowInBackMapper.listVoByBorrowInId(borrowInId);
    }


    private void processBorrowIn(BorrowInBackVo borrowInBackVo){
        List<BorrowInGoods> borrowInGoodsList = new ArrayList<>();
        for (BorrowInBackGoodsVo vo :borrowInBackVo.getBorrowInBackGoodsVoList()){
            BorrowInGoods entity = new BorrowInGoods();
            entity.setId(vo.getBorrowInGoodsId());
            entity.setBorrowInId(borrowInBackVo.getBorrowInId());
            entity.setGoodsId(vo.getGoodsId());
            entity.setNumber(vo.getNumber());
            entity.setHasNumber(vo.getNumber());
            entity.setInGoodsNumber(new BigDecimal(0));
            entity.setWaitNumber(vo.getNumber().negate());
            entity.setUnitId(vo.getUnitId());
            entity.setTimes(vo.getTimes());
            entity.setPrice(new BigDecimal(0));
            entity.setAmount(new BigDecimal(0));
            entity.setCompanyId(borrowInBackVo.getCompanyId());
            borrowInGoodsList.add(entity);
        }
        borrowInGoodsMapper.insertBatch(borrowInGoodsList);

        BorrowIn temp = new BorrowIn();
        temp.setId(borrowInBackVo.getBorrowInId());

        long totalWaitInNumber = borrowInGoodsMapper.countWaitNumber(borrowInBackVo.getBorrowInId());
        long number = borrowInGoodsMapper.countNumber(borrowInBackVo.getBorrowInId());
        if(totalWaitInNumber > 0) {

            if (number == totalWaitInNumber){//未归还
                temp.setInStatus(1);
            } else {//部分归还
                temp.setInStatus(2);
            }
        } else {
            //全部归还
            temp.setInStatus(3);
        }
        borrowInMapper.updateById(temp);
    }

    /**
     * 库存处理
     * @param borrowInBackVo
     */
    private void processGoodsStock(BorrowInBackVo borrowInBackVo){
        List<GoodsStock> goodsStockList = new ArrayList<>();
        for (BorrowInBackGoodsVo vo : borrowInBackVo.getBorrowInBackGoodsVoList()) {
            BigDecimal number = vo.getNumber().multiply(vo.getTimes());
            number = number.negate();

            //库存
            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setId(UuidUtils.getUUID());
            goodsStock.setGoodsId(vo.getGoodsId());
            goodsStock.setHouseId(borrowInBackVo.getHouseId());
            goodsStock.setNum(number); //数量*换算倍数
            goodsStock.setTime(new Date());
            goodsStock.setCompanyId(borrowInBackVo.getCompanyId());
            goodsStockList.add(goodsStock);
        }

        goodsStockMapper.insertBatch(goodsStockList);
    }

    private void processHouseWaitOut(BorrowInBackVo borrowInBackVo) {
        HouseWaitOut houseWaitOut = new HouseWaitOut();
        houseWaitOut.setId(UuidUtils.getUUID());
        houseWaitOut.setBorrowInBackId(borrowInBackVo.getId());
        houseWaitOut.setHouseId(borrowInBackVo.getHouseId());
        houseWaitOut.setClose(false);
        houseWaitOut.setStatus(1);//待出库
        houseWaitOut.setType(5);//设置类型为借入归还
        houseWaitOut.setCompanyId(borrowInBackVo.getCompanyId());
        houseWaitOut.setTime(borrowInBackVo.getTime());
        houseWaitOutMapper.insert(houseWaitOut);

        List<HouseWaitOutGoods> houseWaitOutGoodsList = new ArrayList<>();
        for (BorrowInBackGoodsVo vo : borrowInBackVo.getBorrowInBackGoodsVoList()) {
            HouseWaitOutGoods houseWaitOutGoods = new HouseWaitOutGoods();
            houseWaitOutGoods.setId(UuidUtils.getUUID());
            houseWaitOutGoods.setHouseWaitOutId(houseWaitOut.getId());
            houseWaitOutGoods.setBorrowInBackGoodsId(vo.getId());
            houseWaitOutGoods.setGoodsId(vo.getGoodsId());
            houseWaitOutGoods.setHasOutNumber(new BigDecimal(0));
            houseWaitOutGoods.setWaitOutNumber(vo.getNumber());
            houseWaitOutGoods.setNumber(vo.getNumber());
            houseWaitOutGoods.setUnitId(vo.getUnitId());
            houseWaitOutGoods.setTimes(vo.getTimes());
            houseWaitOutGoods.setCompanyId(borrowInBackVo.getCompanyId());
            houseWaitOutGoodsList.add(houseWaitOutGoods);
        }
        houseWaitOutGoodsMapper.insertBatch(houseWaitOutGoodsList);
    }
}
